﻿using System;
using System.Reflection;

namespace Cubicle.Core.Processors
{

    public static class RequestTypeExtensions
    {
        public static BindingFlags ToBindingFlags(this RequestType flags)
        {
            var result = BindingFlags.Public;
            if (flags.HasFlag(RequestType.Get) && flags.HasFlag(RequestType.Property))
                result = result | BindingFlags.GetProperty;
            if (flags.HasFlag(RequestType.Set) && flags.HasFlag(RequestType.Property))
                result = result | BindingFlags.SetProperty;
            if (flags.HasFlag(RequestType.Get) && flags.HasFlag(RequestType.Field))
                result = result | BindingFlags.GetField;
            if (flags.HasFlag(RequestType.Set) && flags.HasFlag(RequestType.Field))
                result = result | BindingFlags.SetField;
            if (flags.HasFlag(RequestType.Virtual))
                result = result | BindingFlags.Instance;
            if (flags.HasFlag(RequestType.Static))
                result = result | BindingFlags.Static;
            if (flags.HasFlag(RequestType.Construct))
                result = result | BindingFlags.CreateInstance;
            return result;
        }

        public static RequestType Subtract(this RequestType current, RequestType flags)
        {
            return current ^ (current & flags);
        }
    }

    [Flags]
    public enum RequestType
    {
        Universal = 0,
        Virtual = 1,
        Static = 2,
        Property = 4,
        Field = 8,
        Method = 16,
        Construct = 32,
        Get = 64,
        Set = 128,
        CustomConstruct = 256,
        Completed = 512,
        Failed = 1024,
        Aborted = 2048,

        AllNonSpecial = 255,
        AllIncoming = 511,
        All = 4095,

        VirtualFieldGet = 73,
        VirtualFieldSet = 137,
        StaticFieldGet = 74,
        StaticFieldSet = 138,
        VirtualPropertyGet = 69,
        VirtualPropertySet = 133,
        StaticPropertyGet = 70,
        StaticPropertySet = 134,
        AllProperty = 199,
        AllField = 203,
        AllMethod = 19
    }
}